import 'package:flutter/material.dart';
import 'package:flutter_syt/api/api.dart';
import 'package:flutter_syt/domain/list/list_page.dart';
import 'package:flutter_syt/domain/waystation/way_station_search_page.dart';
import 'package:flutter_syt/domain/widgets/stupid_tabs.dart';
import 'package:flutter_syt/domain/widgets/tab_indicator.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/utils/click_utils.dart';
import 'package:yyy_route_annotation/yyy_route_annotation.dart';

import 'map_page.dart';
import 'way_station_news_page.dart';

@RoutePage(name: "volunteerWayStation")
class VolunteerWayStationPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _VolunteerWayStationPageState();
}

class _VolunteerWayStationPageState extends State<VolunteerWayStationPage>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;

  final List<ScrollerTabEntity> tabs = [
    ScrollerTabEntity.create('1', S().Sites, 1, ""),
    ScrollerTabEntity.create('2', S().News, 1, "")
  ];

  @override
  void initState() {
    _tabController =
        TabController(initialIndex: 0, length: tabs.length, vsync: this);
    super.initState();
  }

  Stream<List<MarkerEntity>> loadStationData() async* {
    var list = await API.shared.volunteer.houseList();
    yield list
        .map((e) => MarkerEntity()
          ..id = e.id
          ..lat = e.lat
          ..long = e.long
          ..name = e.name
          ..address = e.address)
        .toList();
  }

  @override
  Widget build(BuildContext context) {
    // NormalTabs(tabs, _tabController),
    return Scaffold(
      appBar: AppBar(
        iconTheme: IconThemeData(color: Colors.black),
        // automaticallyImplyLeading: false,
        backgroundColor: Colors.white,
        centerTitle: true,
        actions: [
          SizedBox(
            width: 44,
          )
        ],
        leading: GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: ClickUtils.debounce(() => Navigator.pop(context)),
          child: Container(
            width: 44,
            height: 44,
            child: Center(
              child: Icon(
                Icons.arrow_back_ios_sharp,
                size: 16,
              ),
            ),
          ),
        ),
        title: Container(
          width: double.infinity,
          child: NormalTabs(tabs, _tabController),
        ),
        elevation: 1,
      ),
      body:
          // Expanded(
          // child:
          TabBarView(
        physics: NeverScrollableScrollPhysics(),
        controller: _tabController,
        children: <Widget>[
          StreamBuilder<List<MarkerEntity>>(
            initialData: [],
            stream: loadStationData(),
            builder: (BuildContext context,
                AsyncSnapshot<List<MarkerEntity>> snapshot) {
              if (snapshot.data!.isEmpty) {
                return Container();
              }
              return WayStationSearchPage(0.0, 0.0);
              // return MapPage(
              //   markers: snapshot.data!,
              //   locationCenter: true,
              //   markerCenter: true,
              // );
            },
          ),
          WayStationNewsListPage(),
        ],
      ),
      // ),
    );
  }
}
